package com.sjx.boundarycallback.ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.os.Bundle;
import android.util.Log;

import com.sjx.boundarycallback.R;
import com.sjx.boundarycallback.databinding.ActivityThreadPoolBinding;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolActivity extends AppCompatActivity {

    private ActivityThreadPoolBinding mBinding;
    //创建基本线程池
    private final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(4, 5, 1, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(100));

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_thread_pool);
        useThreadPool();
    }

    private void useThreadPool() {
        /**
         * 基本线程池使用
         */
        mBinding.btnUseThreadPool.setOnClickListener(v -> {

            for (int i = 0; i < 30; i++) {
                final int finali = i;
                Runnable runnable = () -> {
                    try {
                        Thread.sleep(2000);
                        Log.d("sjxsjxThread", "run: " + finali);
                        Log.d("sjxsjx当前线程：", Thread.currentThread().getName());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                };
                threadPoolExecutor.execute(runnable);
            }
        });
    }

}